package com.syq.propxy;

import jdk.nashorn.internal.ir.CallNode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @ClassName: MyAspect
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/11 11:46
 */

@Component
@Aspect
public class MyAspect {

    @Before(value = "execution(public * com.syq.service.impl.UserServiceImpl.zufang())")
    public void beforeAdvice(){
        System.out.println("找中介");
        System.out.println("中介带看房");
    }

    @AfterReturning(value = "execution(public * com.syq.service.impl.UserServiceImpl.zufang(..))",returning = "r")
    public void afterAdvice(Object r){
        System.out.println("核心方法返回值"+r);
        System.out.println("维修");
        System.out.println("掏钱");
    }

    @AfterThrowing(value = "execution(public * com.syq.service.impl.UserServiceImpl.zufang())",throwing = "e")
    public void exceptionAdvice(Exception e){
        System.out.println(e.getMessage());
    }

    public String aroundAdvice(Object r, ProceedingJoinPoint jp){
        MethodSignature methodSignature= (MethodSignature) jp.getSignature();
        Method method = methodSignature.getMethod();
        Class returnType = methodSignature.getReturnType();

        Object[] args = jp.getArgs();
        Object name = args[0];
        System.out.println("around接受参数:"+name);
        args[0]="10086";

        System.out.println("找中介");
        System.out.println("中介带看房");

        Object result=null;
        try {
            result= jp.proceed(args);

            System.out.println(result);
            System.out.println("维修");
            System.out.println("掏钱");

            return "我不是房东";
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("异常"+e.getMessage());
        }
        return null;

    }
}
